package com.gzy.studentsociety.dao;

import com.gzy.studentsociety.entity.Student;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


public class StudentDaoImpl extends BaseDao implements StudentDao {
    @Override
    // 通过学号和密码来判断是否存在该学生
    public Student login(String acc, String pwd) throws SQLException {
        QueryRunner qr = new QueryRunner(getDataSource());
        String sql = "SELECT * FROM student WHERE studentNumber = ? AND password = ?";
        Student student = qr.query(sql, new BeanHandler<Student>(Student.class), acc, pwd);
        return student;
    }

    // 通过学号 查询学生信息
    public Student findByNumber(String number) throws SQLException {
        QueryRunner qr = new QueryRunner(getDataSource());
        String sql = "select * from student where studentNumber = ?";
        return qr.query(sql,new BeanHandler<Student>(Student.class),number);
    }

    // 通过id 查询学生信息
    public Student findById(Integer id) throws SQLException {
        QueryRunner qr = new QueryRunner(getDataSource());
        String sql = "select * from student where studentId = ?";
        return qr.query(sql,new BeanHandler<Student>(Student.class),id);
    }

    // 通过姓名和学号来查询信息
//    public List<Student> queryStudent(String name, String number) throws SQLException {
//        StringBuffer buffer  = new StringBuffer("select * from student where 1 = 1 ");
//        QueryRunner qr = new QueryRunner(getDataSource());
//        if(name != null && !name.isEmpty()){
//            buffer.append(" and studentName like concat('%',?,'%')");
//        }else if(number != null && !number.isEmpty()){
//            buffer.append(" and studentNumber like concat('%',?,'%')");
//        }
//        List<Student> students = null;
//        if (name != null && name != "" && (number == null || number == "")){
//            students = qr.query(buffer.toString(),new BeanListHandler<Student>(Student.class),name);
//        }else if (number != null && number != "" && (name == null || name == "")){
//            students = qr.query(buffer.toString(),new BeanListHandler<Student>(Student.class),number);
//        }else if (name != null && name != "" && number != null && number != "" ){
//            students = qr.query(buffer.toString(),new BeanListHandler<Student>(Student.class),name,number);
//        }else{
//            students = qr.query(buffer.toString(),new BeanListHandler<Student>(Student.class));
//        }
//        return students;
//    }
    // 学号或姓名查询学生
    public List<Student> queryStudent(String name, String number) throws SQLException {
        StringBuffer buffer  = new StringBuffer("select * from student where 1 = 1 ");
        QueryRunner qr = new QueryRunner(getDataSource());

        // 定义一个列表来存储参数
        List<Object> params = new ArrayList<>();

        // 如果 name 不为空，则添加 name 查询条件和参数
        if (name != null && !name.isEmpty()) {
            buffer.append(" and studentName like concat('%', ?, '%')");
            params.add(name);
        }
        // 如果 number 不为空，则添加 number 查询条件和参数
        if (number != null && !number.isEmpty()) {
            buffer.append(" and studentNumber like concat('%', ?, '%')");
            params.add(number);
        }

        // 执行查询并将参数列表转换为数组
        return qr.query(buffer.toString(), new BeanListHandler<>(Student.class), params.toArray());
    }

    public static void main(String[] args) {
        StudentDao dao = new StudentDaoImpl();
        try {
            Student student = dao.login("2022034567", "123456");
            System.out.println(dao.queryStudent("", "2"));
        } catch (SQLException e) {
            e.printStackTrace(); // 捕获并打印异常信息
        }
    }
}
